<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Inv_cost_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function get_inv_items(){
      $sql="SELECT * FROM inv_items ii WHERE ii.costing_flag = 'Y' AND ii.enable_flag = 'Y' ";
      $query = $this->db->query($sql);
      return $query->result();
    }
    public function insert_cost($data){
        $this->db->query("call inv_item_cost_ins_p(?,?,?,?,?,@id,@err,@msg)",$data);
        $que = $this->db->query("SELECT @id as id ,@err as error , @msg as msg");
        echo json_encode($que->result());
      // $this->db->insert('inv_item_cost', $data);

      //   if ($this->db->affected_rows() > 0) {
      //       echo json_encode("true");
      //   } else {
      //       echo json_encode("false");
      //   }
    }
    public function update_cost($data){
        $this->db->query("call inv_item_cost_upd_p(?,?,?,?,?,?,?,@err,@msg)",$data);
        $que = $this->db->query("SELECT @err as error , @msg as msg");
        echo json_encode($que->result());
        // $this->db->where('cost_id', $id);
        // $this->db->update('inv_item_cost', $data);
        // if ($this->db->affected_rows() > 0) {
        //     echo json_encode("true");
        // } else {
        //     echo json_encode("false");
        // }
    }
    public function get_cost_data($id){
      $this->db->where('cost_id',$id);
      $q = $this->db->get('inv_item_cost_v');
      return $q->result();
    }
    public function search_cost_DB($item_num, $effect_date,$c,$f){
      $and;$dates="";$setter=0;
          if($item_num != "" && $effect_date != ""){ $setter = 1;
            $and = "WHERE tbl.id LIKE '".$item_num."' AND tbl.effect_date LIKE '".$effect_date."'"; 
          }else if($item_num == "" && $effect_date != ""){ $setter = 1;
            $and = "WHERE tbl.effect_date LIKE '".$effect_date."'";
          }else if($item_num != "" && $effect_date == ""){ $setter = 1;
            $and = "WHERE tbl.id LIKE '".$item_num."'";
          }else{
            $and = "WHERE tbl.id LIKE '".$item_num."'";
          }
            if($c == "Y" && $f =="Y"){
              $dates = " AND iic.effective_date >= NOW() OR iic.effective_date <= NOW() ";
            }else if($c=="N" && $f == "Y"){
              $dates = " AND iic.effective_date >= NOW()";
            }else if($c == "Y" && $f == "N"){
              $dates = " AND iic.effective_date <= NOW() ";
            }
          

          //$query = $this->db->get_where('inv_uom', array('module_code' => $modulecode, 'module_name' => $modulename), 1);
          $sql="SELECT tbl.cost_id cost_id, tbl.item_num item_num, tbl.effect_date effect_date, tbl.full_cost full_cost FROM (SELECT iic.cost_id cost_id, ii.item_num item_num, iic.effective_date effect_date, iic.full_cost full_cost,ii.item_id id FROM inv_items ii INNER JOIN inv_item_cost iic on ii.item_id = iic.item_id WHERE ii.costing_flag = 'Y'AND ii.enable_flag = 'Y' ".$dates.")tbl ".$and." ";
          $query = $this->db->query($sql);
          if($query->num_rows() != 0){
            return $query->result();
          }
          else {
            $result = "No results found.";
            return $result;
          }
    }
}